# -*- coding: utf-8 -*-
import scrapy
import re
import time
from paper.items import PaperItem


class WeatherSpider(scrapy.Spider):
    name = 'weather'
    allowed_domains = ['forecast.weather.com.cn']
    # 今日天气
    start_urls = ['http://forecast.weather.com.cn/town/weather1dn/101080708001.shtml',
                  'http://forecast.weather.com.cn/town/weather1dn/101080708002.shtml#input',
                  'http://forecast.weather.com.cn/town/weather1dn/101080708003.shtml#input',
                  'http://forecast.weather.com.cn/town/weather1dn/101080708004.shtml#input',
                  'http://forecast.weather.com.cn/town/weather1dn/101080708005.shtml#input',
                  'http://forecast.weather.com.cn/town/weather1dn/101080708006.shtml#input',
                  'http://forecast.weather.com.cn/town/weather1dn/101080708007.shtml#input',
                  'http://forecast.weather.com.cn/town/weather1dn/101080708008.shtml#input']

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.seven_days_urls = 'http://d1.weather.com.cn/sk_2d/101080708.html?_=1586827793138'

    def parse(self, response):
        tep = response.xpath(
            "/html/body/div[@class='L_weather']/div[@class='todayModel weatherBg01']/div[@class='todayLeft']")
        item = PaperItem()
        """今日天气"""
        # 气温
        item['temp'] = tep.xpath(".//div[@class='tempDiv']/span[@class='temp']/text()").extract_first()
        # 相对湿度 - 匹配出湿度数字
        item['humi'] = re.findall(r'\b\d+\b', tep.xpath("p[2]/span/text()").extract_first())
        item['humi'] = item['humi'][0]
        win_or_dir = tep.xpath("p[1]/span/text()").extract_first()
        # 风速
        win_len = re.findall(r'\d+', win_or_dir)
        if len(win_len) > 0:
            item['wind'] = win_len[0]
        # 风向
        # 匹配中文
        win_list = re.findall(r'[\u4e00-\u9fa5]', win_or_dir)
        win_list = win_list[0: len(win_list) - 1]
        # 转换为字符串
        win_list = ''.join(win_list)
        item['wind_dir'] = win_list
        # 当前页面的乡镇名称
        city_x = response.xpath(
            "/html/body/div[@class='weather_location']/div[@class='areaSelect webox'][3]/a/text()").extract_first()
        item['city_name'] = city_x
        # 日期 - 获取当前日期
        item['which_day'] = time.strftime("%Y-%m-%d", time.localtime())
        # 状态值
        item['status'] = 1
        yield item
        pass
